﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Ionic.Zip;

namespace Mole
{

    public class ZipInput
    {
        private String _filePath = "";
        private String _zipPath = ".";
        private String _comment = "";


        public String FilePath 
        {
            get
            {
                return _filePath;
            }
            set
            {
                _filePath = value;
            }
        }
        public String ZipPath
        {
            get
            {
                return _zipPath;
            }
            set
            {
                _zipPath = value;
            }
        }
        public String Comment
        {
            get
            {
                return _comment;
            }
            set
            {
                _comment = value;
            }
        }

        public ZipInput(String filePath, String comment)
        {
            FilePath = filePath;
            Comment = comment;
        }

    }


    public static class ZipFiles
    {

        /// <summary>
        /// Creates zip file and returns errors
        /// </summary>
        /// <param name="filepaths"></param>
        /// <param name="zipSavePath"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static String Create(ZipInput[] files, string zipSavePath, string password)
        {
            string error = "";

            try
            {
                using (ZipFile zip = new ZipFile())
                {
                    if (!String.IsNullOrEmpty(password))
                    {
                        zip.Password = password;
                    }

                    if (files != null)
                    {
                        foreach (ZipInput file in files)
                        {
                            try
                            {
                                //zip.AddFile(file.filepath, file.zippath);
                                ZipEntry e = zip.AddFile(file.FilePath, file.ZipPath);
                                e.Comment = file.Comment;
                            }
                            catch (Exception ex)
                            {
                                System.Diagnostics.Debug.Write(ex.Message);
                                error += ex.Message + "\r\n";
                            }
                        }
                    }
                    zip.Save(zipSavePath);
                }

                return error;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Write(ex.Message);
                throw;
            }
        }
    }
}
